

# 确定日期
def find_recent_date_auto(database, table_name, update_datetime):
    #
    s_update_datetime = update_datetime.strftime('%Y-%m-%d')
    sql_str = "select max(date) from derivative_data." + table_name + " where date <=" + "'" + s_update_datetime + "'"
    # print(sql_str)
    max_date = database.ExecuteSQL("derivative_data", sql_str)
    recent_date = max_date[0][0]
    return recent_date


def float_2_percentage(document, fields=[]):
    #
    if len(fields) == 0:
        fields = document.keys()
    #
    for field in fields:
        value = document.get(field)
        if value != None:
            document[field] = str(format(value * 100,  ".2f")) + " %"
        else:
            document[field] = None


def float_formatted(document, fields=[], keep_float=2):
    #
    if len(fields) == 0:
        fields = document.keys()
    #
    for field in fields:
        value = document.get(field)
        if value != None:
            document[field] = format(value,  ".2f")
        else:
            document[field] = None


def df_float_formated(df, fields=[], keep_float=2):
    #
    def func(value):
        if value != None:
            value = format(value,  "." + str(keep_float) + "f")
            return value
        else:
            return None
    #
    for field in fields:
        if field not in df.columns:
            continue
        else:
            df[field] = df[field].apply(lambda x: func(x))


if __name__ == '__main__':

    doc = {"aaa": 0.0123456}
    float_2_percentage(doc)
    print(doc)
